package com.jiayuge.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayuge.server.common.entity.RespBean;
import com.jiayuge.server.common.entity.RespPageBean;
import com.jiayuge.server.pojo.Employee;
import com.jiayuge.server.mapper.EmployeeMapper;
import com.jiayuge.server.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jiayu.Yang
 * @since 2021-11-15
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 功能: 获取所有员工(分页)  |  获取所有员工(分页)  |  数据库: 增 | 删 | 改 | 查
     * * @param currentPage
     *
     * @param size           分页单位
     * @param employee
     * @param beginDateScope
     * @return {@link RespPageBean}
     * @Date 2021/11/26
     * @author jiayu.Yang
     */
    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        //开启分页
        Page<Employee> page = new Page<>(currentPage, size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
        RespPageBean respPageBean = new RespPageBean(employeeByPage.getTotal(), employeeByPage.getRecords());
        return respPageBean;
    }

    /**
     * 功能: 获取最大工号  |  获取最大工号  |  数据库: 增 | 删 | 改 | 查
     * * @param
     *
     * @return {@link RespBean}
     * @Date 2021/11/29
     * @author jiayu.Yang
     */
    @Override
    public RespBean maxWorkID() {
        /*
        selectMaps:查询结果返回map类型
            select：可以直接写查询的结果，这个结果名作为key存档map中
         */
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        //由于数据的构造，需要将value转换后在进行+1,再转回原本的类型 String.format（“%08d”）格式化，防止乱码；
        return RespBean.success(null, String.format("%08d", Integer.parseInt(maps.get(0).get("max(workID)").toString()) + 1));
    }

    /**
     * 功能: 添加员工  |  添加员工  |  数据库: 增 | 删 | 改 | 查
     * * @param employee
     *
     * @return {@link RespBean}
     * @Date 2021/11/29
     * @author jiayu.Yang
     */
    @Override
    public RespBean addEmp(Employee employee) {
        //计算合同期限
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        //返回结果以天为单位
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        //创建一个保留两位小数的浮点数
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days / 365.00)));

        //插入员工数据
        if (1 == employeeMapper.insert(employee)) {
            //添加员工成功，给给员工发送邮件信息
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);
//            rabbitTemplate.convertAndSend("mail.welcome",emp);
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    /**
     * 功能: 查询员工  |  查询员工  |  数据库: 增 | 删 | 改 | 查
     * * @param id
     *
     * @return {@link List< Employee>}
     * @Date 2021/11/29
     * @author jiayu.Yang
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    /**
     * 功能: 获取所有的员工的账套  |  获取所有的员工的账套  |  数据库: 增 | 删 | 改 | 查
     * * @param currentPage
     *
     * @param size
     * @return {@link RespPageBean}
     * @Date 2021/12/7
     * @author jiayu.Yang
     */
    @Override
    public RespPageBean getEmployeeWithSalary(Integer currentPage, Integer size) {
        //开启分页
        Page<Employee> page = new Page<>(currentPage, size);
        IPage<Employee> employeeIPage=employeeMapper.getEmployeeWithSalary(page);
        RespPageBean respPageBean = new RespPageBean(employeeIPage.getTotal(),employeeIPage.getRecords());
        return respPageBean;
    }
}
